<?php

defined('_JEXEC') or die('Restricted access');
use Joomla\Registry\Registry;
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class ConsoleModelFunnelItem extends JModelList
{
    /**store
     * @param $inputs
     * @return bool
     */
    public function store($inputs) {
        $inputs['appkey'] = self::getCode();
        $trackDate = JFactory::getDate()->format('Y-m-d H:i:s');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $query->insert('pp_funnel_item')
            ->columns(
                array(
                    $db->quoteName('funnel_id'), $db->quoteName('event_id') , $db->quoteName('position')
                )
            )
            ->values(' "'.$inputs['funnel_id'].'",  "'.$inputs['event_id'].'" ,  "'.$inputs['position'].'"' );

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    /**update
     * @param $inputs
     * @return bool
     */
    public function update($inputs) {
        $id = $inputs['id'];
        $db = $this->getDbo();
        $query = $db->getQuery(true)
            ->update('pp_funnel_item')
            ->set("event_id = '".trim($inputs['event_id'])."'")
            ->where('id = ' . (int) $id);

        // Set the query and execute the update.
        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }

        return true;
    }


    protected function getListQuery()
    {
        $app = JFactory::getApplication('site');
        $funnel_id = $app->input->get('funnel_id');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $params = $app->getParams();
        $this->setState('params', $params);
        $query->select(
            'fi.*,fc.name,ec.event_name'
        )
            ->from('pp_funnel_item as fi')
            ->join('LEFT', 'pp_funnel_cfg AS fc ON fc.id = fi.funnel_id')
            ->join('LEFT', 'pp_event_cfg AS ec ON ec.event_id = fi.event_id')
            ->where('fi.funnel_id='.$funnel_id);
        $query->order('fi.id DESC');

        return $query;
    }

    public function getApp()
    {
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_app')
            ->where('account_id='.$user->id);
        $query->order('id asc');
        $db->setQuery($query);
        $items = $this->_getList($query);
        return $items;
    }

    public function getEvents()
    {
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select(
            'ec.*,app.name'
        )
            ->from('pp_event_cfg as ec')
            ->join('INNER', 'pp_app AS app ON app.id = ec.app_id')
            ->where('app.account_id='.$user->id);
        $db->setQuery($query);
        $items = $this->_getList($query);
        return $items;
    }

    public function getItem($id = null)
    {

        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_funnel_item ')
            ->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }

    public function getFunnel($id = null)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_funnel_cfg ')
            ->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }
    /**delete
     * @param $id
     */
    public function delete($id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->delete('pp_funnel_item')

            ->where('id='.$id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    /**获取唯一字符串
     * @return string
     */
    protected static function getCode()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }
}
